package common.core.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据传递帮助类
 * @author 刘波
 *
 */
public class Result implements Serializable{

	private static final long serialVersionUID = 1L;
	public int code;							//状态码
	/**系统操作完成返回参数-->成功:20000(操作)*/
	public static final int code_success=0;
	public static Result success(Object data){
		return new Result(0, "", data);
	}
	public static Result success(){
		return new Result(0, "", "");
	}

	/**系统操作完成返回参数-->失败:-1(自己抛错或者自己认为失败)*/
	public static final int code_fail=-1;
	public static Result fail(String msg){
		return new Result(-1, msg);
	}
	/**系统操作完成返回参数-->失败:2002(非法的请求参数)*/
	public static final int illegal = 2002;
	public static Result illegal(String msg){
		return new Result(-1, msg);
	}
	/**系统操作完成返回参数-->异常:-2(系统抛错)*/
	public static final int code_exception=-2;
	public static Result exception(String msg){
		return new Result(-2, msg);
	}
	/**系统操作完成返回参数-->没有数据:-3(没有数据)*/
	public static final int code_not_data=-3;
	public static Result notData(String msg){
		return new Result(-3, msg);
	}
	/**系统操作完成返回参数-->没有数据:401(没有权限)*/
	public static final int code_not_authority=401;
	public static Result notAuthority(String msg){
		return new Result(code_not_authority, msg);
	}
	
	/**系统操作完成返回参数-->服务调用失败:-5(调用其他服务失败)*/
	public static final int code_service_error=-5;
	public static Result servicError(String msg){
		return new Result(-5, msg);
	}
	/**逻辑判断完成--> 出现偏差,但可以继续 */
	public static final int code_continuation=-10;
	public static Result continuation(String msg){
		return new Result(-10, msg);
	}
	
	public String msg;                          //信息 =msg
	
	private Object data;						//数据
	
	public Result(){
		super();
		this.code=0;
		this.msg="";
		this.data="";
	}
	public Result(int code){
		super();
		this.code=code;
		this.msg="";
		this.data="";
	}
	public Result(int code,String msg){
		super();
		this.code=code;
		this.msg=msg;
		this.data="";
	}
	public Result(int code,String msg,Object data){
		super();
		this.code=code;
		this.msg=msg;
		this.data=data;
	}
	public Result(int code,String msg,Object data,String system){
		super();
		this.code=code;
		this.msg=msg;
		this.data=data;
	}
	public static Result result(int code){
		return new Result(code);
	}
	public static Result result(int code,String msg){
		return new Result(code, msg);
	}
	public static Result result(int code,String msg,Object data){
		return new Result(code, msg, data);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@SuppressWarnings("unchecked")
	public <T> T getData() {
		return (T)data;
	}
	@SuppressWarnings("all")
	public <T> T getData(Class<T> clazz) {
		if(data instanceof Map){
			return CommonUtil.convertMapToBean((Map)data, clazz);
		}
		return (T)data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	/**
	 * 数据为空
	 * @return
	 */
	public boolean isEmpty(){
		if(CommonUtil.isEmpty(data)){
			return true;
		}
		return false;
	}
	/**
	 * 数据不为空
	 * @return
	 */
	public boolean isNotEmpty(){
		return !isEmpty();
	}
	/**
	 * TransmitUtil.code_success相同
	 * @return
	 */
	public boolean isSuccess(){
		if(Result.code_success==code){
			return true; 
		}
		return false;
	}
	/**
	 * TransmitUtil.code_success不相同
	 * @return
	 */
	public boolean isNotSuccess(){
		return !isSuccess();
	}
	/**
	 * TransmitUtil.code_fail相同
	 * @return
	 */
	public boolean isFail(){
		if(Result.code_fail==code){
			return true; 
		}
		return false;
	}

	/**
	 * TransmitUtil.code_exception相同
	 * @return
	 */
	public boolean isException(){
		if(Result.code_exception==code){
			return true; 
		}
		return false;
	}

	/**
	 * 是否可以继续
	 * @return
	 */
	public boolean isContinue(){
		if(Result.code_continuation==code|| 
				Result.code_success==code){
			return true;
		}
		return false;
	}
	public boolean isNotAuthority(){
		if(Result.code_not_authority==code){
			return true;
		}
		return false;
	}
	@Override
	public String toString() {
		return CommonUtil.convertObjectToJson(this);
	}
	
	public Map<String , Object > toMap(){
		Map<String , Object > map = new HashMap<String , Object>();
		map.put("code", code);
		map.put("msg", msg);
		map.put("data", data);
		return map;
	}

}


